<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\yperp\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\yperp\common\Tools;
use app\yperp\job\GoodsListAsync;
use app\yperp\logic\GoodsListAsync as LogicGoodsListAsync;
use app\yperp\model\Goods;
use app\yperp\model\Supplier as SupplierModel;
use think\Db;
use think\facade\View;

/**
 * 供应商控制器
 * @package app\yperp\admin
 */
class Supgoods extends Admin
{
    /**
     * 供应商列表
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {

        // 查询
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder('id desc');
        // 数据列表
        $data_list = Goods::where($map)->where('id > 0')->order($order)->paginate();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTips('debug:' . Db::getLastSql(), 'warning')

            ->setSearch([
                'supplierId' => '供应商ID',
                'supplierName' => '供应商名称',
                'itemName' => '商品名称',
                'itemSpuId' => '商品供应链ID',
                'styleCode' => '资料编码',
                'itemCoId' => '商品id'
            ]) // 设置搜索框
            ->addColumns([ // 批量添加数据列
                ['id', 'ID'],
                ['supplierId', '供应商ID', 'text'],
                ['supplierName', '供应商名称', 'text'],
                ['itemCoId', '商品id', 'text'],
                ['itemName', '商品名称', 'text'],
                ['itemSpuId', '商品供应链ID', 'text'],
                ['styleCode', '资料编码', 'text'],
                ['saleThirty', '月销量', 'text'],
                ['saleThirtyDesensitized', '月销量', 'text'],

                ['right_button', '操作', 'btn']
            ])
            ->addRightButton('view', [
                'title' => '预览',
                'icon'  => 'fa fa-fw fa-magic',
                'class' => 'btn btn-success',
                'href'  => url('view', ['id' => '__id__'])
            ], ['area' => ['600px', '90%']])
            ->addRightButton('link', [
                'title' => '聚水潭',
                'icon'  => 'fa fa-fw fa-magic',
                'class' => 'btn btn-success',
                'target' => '_blank',
                'href'  => 'https://gyl.scm121.com/channel/goodsDetailNew/__itemSpuId__?itemCoId=__itemCoId__'
            ])
            ->addRightButton('link', [
                'title' => '更新',
                'icon'  => 'fa fa-fw fa-code-fork',
                'class' => 'btn btn-success',
                'href'  => url('add', ['supplierId' => '__supplierId__', 'itemSpuId' => '__itemSpuId__', 'itemCoId' => '__itemCoId__'])
            ])
            ->addOrder(['id' => 'asc', 'supplierId' => 'asc', 'update_time' => 'desc'])
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染模板
    }


    /**
     * 新增
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        $supplierId = $this->request->param('supplierId');
        $itemSpuId = $this->request->param('itemSpuId');
        $itemCoId = $this->request->param('itemCoId');
        try {
            LogicGoodsListAsync::getInstance()->goodsdetail($itemCoId, $itemSpuId, $supplierId);
            $this->success('获取完成');
        } catch (\Throwable $th) {
            $this->error('获取失败');
        }
    }

    /**
     * 编辑
     * @param null $id 链接id
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'Supplier');
            if (true !== $result) $this->error($result);

            if (SupplierModel::update($data)) {
                // 记录行为
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        }

        $info = SupplierModel::get($id);

        // 显示编辑页面
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['text', 'corp_name', '公司名称'],
                ['text', 'goods_code', '资料编码'],
                ['text', 'goods_title', '商品名称'],
                ['text', 'goods_color', '颜色'],
                ['text', 'goods_color_id', '颜色id'],
                ['text', 'goods_specs', '规格'],
                ['text', 'goods_sku', '商品编码'],
                ['text', 'goods_in_price', '供货价'],
                ['text', 'goods_buy_price', '零售价'],
                ['text', 'goods_retail_price', '分销控价'],
                ['text', 'goods_buy_number', '库存'],
                ['text', 'goods_imgs_file', '图片文件包'],
                ['radio', 'status', '立即启用', '', ['否', '是']]
            ])
            ->setFormData($info)
            ->fetch();
    }

    public function view($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        $goods = Goods::where(['id' => $id])->find();
        $header_imgs = json_decode($goods['itemMainImage'], true);
        $body_imgs = json_decode($goods['itemDetailImages'], true);

        View::assign('header',  $header_imgs);
        View::assign('body', $body_imgs);
        View::assign('goods', $goods);

        return View::fetch();
    }
}
